package com.xxl.shoppingapp.ui.cart;

import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xxl.shoppingapp.R;
import com.xxl.shoppingapp.base.BaseFragment;
import com.xxl.shoppingapp.entity.CommonProduct;
import com.xxl.shoppingapp.entity.ShopCart;
import com.xxl.shoppingapp.ui.adapter.ShopCartAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * @author xuelian.xiong 2018/4/10
 * @Package com.xxl.shoppingapp.ui.main
 * @Title: CartFragment
 * @Description: (用一句话描述该文件做什么)
 * Copyright (c) 传化公路港物流有限公司版权所有
 * Create DateTime: 2018/4/10
 */
public class CartFragment extends BaseFragment {
    @Bind(R.id.rlv_shopcart)
    RecyclerView rlvShopCart;
    @Bind(R.id.tv_shopcart_addselect)
    TextView tvShopCartSelect;
    @Bind(R.id.tv_shopcart_totalprice)
    TextView tvShopCartTotalPrice;
    @Bind(R.id.tv_shopcart_totalnum)
    TextView tvShopCartTotalNum;
    @Bind(R.id.tv_shopcart_submit)
    TextView tvShopCartSubmit;
    @Bind(R.id.ll_pay)
    LinearLayout llPay;
    @Bind(R.id.emtryview)
    View mEmtryView;
    @Bind(R.id.rl_shopcart_have)
    RelativeLayout rlHaveProduct;
    private ArrayList<CommonProduct> orders;
    private List<ShopCart.CartlistBean> mAllOrderList = new ArrayList<>();
    private ArrayList<ShopCart.CartlistBean> mGoPayList = new ArrayList<>();
    private List<String> mHotProductsList = new ArrayList<>();
    private boolean mSelect;
    private ShopCartAdapter mShopCartAdapter;
    private int mCount,mPosition;
    private float mTotalPrice1;
    @Override
    protected void initView(View view) {
        super.initView(view);
        setTitle(getString(R.string.cart));
        orders = new ArrayList<>();
        init();
    }

    private void init() {
        mEmtryView.setVisibility(View.GONE);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
        llPay.setLayoutParams(lp);


        rlvShopCart.setLayoutManager(new LinearLayoutManager(getActivity()));
        mShopCartAdapter = new ShopCartAdapter(getActivity(),mAllOrderList);
        rlvShopCart.setAdapter(mShopCartAdapter);
        //删除商品接口
        mShopCartAdapter.setOnDeleteClickListener(new ShopCartAdapter.OnDeleteClickListener() {
            @Override
            public void onDeleteClick(View view, int position,int cartid) {
                mShopCartAdapter.notifyDataSetChanged();
            }
        });
        //修改数量接口
        mShopCartAdapter.setOnEditClickListener(new ShopCartAdapter.OnEditClickListener() {
            @Override
            public void onEditClick(int position, int cartid, int count) {
                mCount = count;
                mPosition = position;
            }
        });
        //实时监控全选按钮
        mShopCartAdapter.setResfreshListener(new ShopCartAdapter.OnResfreshListener() {
            @Override
            public void onResfresh( boolean isSelect) {
                mSelect = isSelect;
                if(isSelect){
                    Drawable left = getResources().getDrawable(R.mipmap.shopcart_selected);
                    tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
                }else {
                    Drawable left = getResources().getDrawable(R.mipmap.shopcart_unselected);
                    tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
                }
                float mTotalPrice = 0;
                int mTotalNum = 0;
                mTotalPrice1 = 0;
                mGoPayList.clear();
                for(int i = 0;i < mAllOrderList.size(); i++)
                    if(mAllOrderList.get(i).getIsSelect()) {
                        mTotalPrice += Float.parseFloat(mAllOrderList.get(i).getPrice()) * mAllOrderList.get(i).getCount();
                        mTotalNum += 1;
                        mGoPayList.add(mAllOrderList.get(i));
                    }
                mTotalPrice1 = mTotalPrice;
                tvShopCartTotalPrice.setText("总价：" + mTotalPrice);
                tvShopCartTotalNum.setText("共" + mTotalNum + "件商品");
            }
        });

        //全选
        tvShopCartSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSelect = !mSelect;
                if(mSelect){
                    Drawable left = getResources().getDrawable(R.mipmap.shopcart_selected);
                    tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
                    for(int i = 0;i < mAllOrderList.size();i++){
                        mAllOrderList.get(i).setSelect(true);
                        mAllOrderList.get(i).setShopSelect(true);
                    }
                }else{
                    Drawable left = getResources().getDrawable(R.mipmap.shopcart_unselected);
                    tvShopCartSelect.setCompoundDrawablesWithIntrinsicBounds(left,null,null,null);
                    for(int i = 0;i < mAllOrderList.size();i++){
                        mAllOrderList.get(i).setSelect(false);
                        mAllOrderList.get(i).setShopSelect(false);
                    }
                }
                mShopCartAdapter.notifyDataSetChanged();

            }
        });


    }


    @Override
    protected int getLayout() {
        return R.layout.fragment_cart;
    }

    @Override
    protected void initData() {
        super.initData();
        for(int i = 0;i < 2;i ++){
            ShopCart.CartlistBean sb = new ShopCart.CartlistBean();
//            sb.setShopId(1);
            sb.setPrice("1.0");
//            sb.setDefaultPic("http://img2.3lian.com/2014/c7/25/d/40.jpg");
            sb.setProductName("狼牙龙珠鼠标");
            sb.setShopName("狼牙龙珠");
            sb.setColor("蓝色");
            sb.setCount(2);
            mAllOrderList.add(sb);
        }

        for(int i = 0;i < 2;i ++){
            ShopCart.CartlistBean sb = new ShopCart.CartlistBean();
//            sb.setShopId(2);
            sb.setPrice("1.0");
//            sb.setDefaultPic("http://img2.3lian.com/2014/c7/25/d/40.jpg");
            sb.setProductName("达尔优鼠标");
            sb.setShopName("达尔优");
            sb.setColor("绿色");
            sb.setCount(2);
            mAllOrderList.add(sb);
        }
        isSelectFirst(mAllOrderList);
    }

    public static void isSelectFirst(List<ShopCart.CartlistBean> list){
        if(list.size() > 0) {
            list.get(0).setIsFirst(1);
            for (int i = 1; i < list.size(); i++) {
//                if (list.get(i).getShopId() == list.get(i - 1).getShopId()) {
//                    list.get(i).setIsFirst(2);
//                } else {
                    list.get(i).setIsFirst(1);
//                }
            }
        }

    }
}
